﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace FrameworkEditor.UITools.UIGenerate
{
    public class UIGenerateEditor : EditorWindow
    {
        private string mUIName = string.Empty;
        private bool mIsSubPanel = false;
        private string mMainPanelName = string.Empty;

        [MenuItem("FrameworkEditor/UIGenerate")]
        public static void UIGenerate()
        {
            EditorWindow.GetWindowWithRect<UIGenerateEditor>(new Rect(0, 0, 300, 200), false, "UIGenerate");
        }

        private void OnGUI()
        {
            GUILayout.Label("请输入要创建UI对象的名称", EditorStyles.boldLabel);
            mUIName = GUILayout.TextField(mUIName);

            mIsSubPanel = GUILayout.Toggle(mIsSubPanel, "是否为子面板");
            if (mIsSubPanel)
            {
                GUILayout.Label("输入主面板名称");
                mMainPanelName = GUILayout.TextField(mMainPanelName);
            }

            if (GUILayout.Button("生成全部UI组件"))
                GenerateAll();

            if (GUILayout.Button("生成全部UI组件(脚步除外)"))
            {
                GeneratePrefab();
                GenerateTextureAndAtlas();
            }

            if (GUILayout.Button("单独生成脚本"))
                GenerateScript();

            if (GUILayout.Button("单独生成Prefab"))
                GeneratePrefab();

            if (GUILayout.Button("单独生成Texture目录和Atlas"))
                GenerateTextureAndAtlas();
        }

        private void GenerateAll()
        {
            if (CheckUIName() && CheckMainUIName())
            {
                GenerateScript();
                GeneratePrefab();
                GenerateTextureAndAtlas();
            }
        }

        private void GenerateScript()
        {
            if (CheckUIName() && CheckMainUIName())
            {
                UIGenerateScript uiGenerateScript = new UIGenerateScript(mUIName, mIsSubPanel, mMainPanelName);
                uiGenerateScript.GenerateScript();
                AssetDatabase.Refresh();
            }
        }

        private void GeneratePrefab()
        {
            if (CheckUIName() && CheckMainUIName())
            {
                PlayerPrefs.SetString("GeneratePrefab_UIName", mUIName);
                PlayerPrefs.SetInt("GeneratePrefab_IsSubPanel", mIsSubPanel ? 1 : 0);
                PlayerPrefs.SetString("GeneratePrefab_MainPanelName", mMainPanelName);
                if (!EditorApplication.isCompiling)
                    GeneratePrefabAsyn();
            }
        }

        [UnityEditor.Callbacks.DidReloadScripts]
        private static void GeneratePrefabAsyn()
        {
            string uiName = PlayerPrefs.GetString("GeneratePrefab_UIName");
            int isSubPanelInt = PlayerPrefs.GetInt("GeneratePrefab_IsSubPanel");
            bool isSubPanel = isSubPanelInt == 0 ? false : true;
            string mainPanelName = PlayerPrefs.GetString("GeneratePrefab_MainPanelName");
            if (!string.IsNullOrEmpty(uiName))
            {
                UIGeneratePrefab uiGeneratePrefab = new UIGeneratePrefab(uiName, isSubPanel, mainPanelName);
                uiGeneratePrefab.GeneratePrefab();
                AssetDatabase.Refresh();
                PlayerPrefs.SetString("GeneratePrefab_UIName", "");
                PlayerPrefs.SetInt("GeneratePrefab_IsSubPanel", 0);
                PlayerPrefs.SetString("GeneratePrefab_MainPanelName", "");
            }
        }

        private void GenerateTextureAndAtlas()
        {
            if (CheckUIName() && CheckMainUIName())
            {
                UIGenerateTextureAndAtlas uiGenerateTextureAndAtlas = new UIGenerateTextureAndAtlas(mUIName, mIsSubPanel, mMainPanelName);
                uiGenerateTextureAndAtlas.GenerateTextureAndAtlas();
                AssetDatabase.Refresh();
            }
        }

        private bool CheckUIName()
        {
            if (string.IsNullOrEmpty(mUIName))
            {
                Debug.LogWarning("UI名称不能为空");
                return false;
            }

            return true;
        }

        private bool CheckMainUIName()
        {
            if (mIsSubPanel && string.IsNullOrEmpty(mMainPanelName))
            {
                Debug.LogWarning("主面板名称不能为空");
                return false;
            }
            return true;
        }
    }
}

